<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Page_Model extends MY_Model {
    public function __construct()
    {
        parent::__construct();
        $this->load->model("entity/page_entity", "Page_Entity");
    }

    public function getPages($publish_flg=1)
    {
        $result = null;
        $this->db->select('id, title, slug, position, order, content, template', true);
        $this->db->order_by('position', 'asc');
        $this->db->order_by('order', 'asc');
        $query = $this->db->get_where($this->table_page_name, array('publish_flg'=>1, 'deleted_at'=> NULL));
        $result = $query->result('Page_Entity');
        return $result;
    }

    public function getPageBySlug($slug, $aCol=array(), $option=array('publish_flg'=>ON, 'deleted_at'=>NULL)){
        $result = null;
        if(count($aCol)>0){
            $this->db->select(implode(',', $aCol), true);
        } else {
            $this->db->select('id, title, slug, position, order, content, template', true);
        }
        $this->db->from($this->table_page_name);
        $this->db->where('slug', $slug);
        $this->db->where($option);
        $result = $this->db->get()->first_row('Page_Entity');
        return $result;
    }

    /**
    * For admin only
    * Get all page for view and update
    */
    public function getAllPage($options=array()){
        $result = null;
        $select = "id, title, slug, position, order, content, template, publish_flg, created_at, updated_at, deleted_at";
        $this->db->select($select, true);
        $this->db->from($this->table_page_name);
        if(count($options)>0){
            $this->db->where($options);
        }
        $result = $this->db->get()->result('Page_Entity');
        return $result;
    }

    /**
     * For admin only
     * update a page
     */
    public function update($page_id, $page){
        $this->db->trans_begin();
        $this->db->where('id', $page_id);
        $this->db->update($this->table_page_name, $page);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    /**
     * For admin only
     * update a page
     */
    public function updateBySlug($page_slug, $page){
        $this->db->trans_begin();
        $this->db->where('slug', $page_slug);
        $this->db->update($this->table_page_name, $page);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    /**
    * For admin only
    * Check valid page title -slug
    */
    public function checkExistsPage($curPageId, $page){
        $this->db->select('id', true);
        $this->db->from($this->table_page_name);
        if($curPageId!=0){
            $this->db->where('id !=', $curPageId);
        }
        $this->db->where("(slug='{$page['slug']}' OR title='{$page['title']}')");
        return $this->db->count_all_results();
    }

    /**
    * For admin only
    * Add new page
    */
    public function insert($page){
        $this->db->trans_begin();
        $this->db->insert($this->table_page_name, $page);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    ///// FOR SEARCH /////

    public function searchProduct($key, $limit, $offset) {
        $this->load->model('entity/product_entity', 'Product_Entity');
        $result = null;
        $aWhere = array(
            'a.deleted_at' => null,
            'b.deleted_at' => null,
            'c.deleted_at' => null,
            'a.active_flg' => 1);
        $this->db->select('
            a.id, a.title, a.cate_id, a.shop_id, a.slug,
            b.id as detail_id, b.code, b.image, b.price_new, b.price_old, b.description,
            c.title as category_title, c.slug as category_slug,
            d.title as shop_title, d.slug as shop_slug,
            e.image_folder AS shop_image_folder
            ', true);
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_category_name . ' c', 'a.cate_id=c.id');
        $this->db->join($this->table_shop_name . ' d', 'a.shop_id=d.id');
        $this->db->join($this->table_user_name . ' e', 'd.user_id=e.id');
        $this->db->where($aWhere);
        $likeq = "a.title LIKE '%{$key}%'";
        $likeq .= " OR a.slug LIKE '%{$key}%'";
        $likeq .= " OR b.code LIKE '%{$key}%'";
        $likeq .= " OR b.price_old LIKE '%{$key}%'";
        $likeq .= " OR b.price_new LIKE '%{$key}%'";
        $likeq .= " OR b.image LIKE '%{$key}%'";
        $likeq .= " OR b.description LIKE '%{$key}%'";
        $likeq .= " OR c.title LIKE '%{$key}%'";
        $likeq .= " OR c.slug LIKE '%{$key}%'";
        $likeq .= " OR d.title LIKE '%{$key}%'";
        $likeq .= " OR d.slug LIKE '%{$key}%'";
        $likeq = "(" . $likeq . ")";
        $this->db->where($likeq);
        $this->db->limit($limit, $offset);
        $this->db->order_by('a.updated_at', 'asc');
        $this->db->order_by('a.created_at', 'asc');
        $result = $this->db->get()->result('Product_Entity');
        return $result;
    }

    public function searchPost($key, $limit, $offset){
        $this->load->model('entity/post_entity', 'Post_Entity');
        $select = "a.id, a.title, a.slug, a.image, a.description, a.created_at, b.title as cate_title, b.slug as cate_slug";
        $this->db->select($select);
        $this->db->from($this->table_post_name . ' AS a');
        $this->db->join($this->table_post_cate_name . ' b', 'b.id=a.post_cate_id');
        $this->db->where('a.publish_flg', ON);
        $this->db->where('a.deleted_at', NULL);
        $this->db->where('b.deleted_at', NULL);
        $likeq = "(a.title LIKE '%{$key}%'";
        $likeq .= " OR a.slug LIKE '%{$key}%'";
        $likeq .= " OR a.description LIKE '%{$key}%'";
        $likeq .= " OR a.content LIKE '%{$key}%'";
        $likeq .= " OR b.title LIKE '%{$key}%'";
        $likeq .= " OR b.slug LIKE '%{$key}%')";
        $this->db->where($likeq);
        $this->db->limit($limit, $offset);
        $this->db->order_by('a.created_at', 'DESC');
        $this->db->order_by('a.updated_at', 'DESC');
        return $this->db->get()->result('Post_Entity');
    }

    public function searchRaoVat($key, $limit, $offset){
        $this->load->model('entity/raovat_entity', 'RaoVat_Entity');
        $select = "a.id, a.title, a.slug, a.image, a.description, a.created_at, b.title as cate_title, b.slug as cate_slug, c.name as place_name";
        $this->db->select($select);
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_category_name . ' b', 'b.id=a.cate_id');
        $this->db->join($this->table_rao_vat_place_name . ' c', 'c.id=a.place_id');
        $this->db->where('a.active_flg', ON);
        $this->db->where('a.deleted_at', NULL);
        $this->db->where('b.deleted_at', NULL);
        $likeq = "(a.title LIKE '%{$key}%'";
        $likeq .= " OR a.slug LIKE '%{$key}%'";
        $likeq .= " OR a.description LIKE '%{$key}%'";
        $likeq .= " OR a.detail LIKE '%{$key}%'";
        $likeq .= " OR c.name LIKE '%{$key}%'";
        $likeq .= " OR b.title LIKE '%{$key}%'";
        $likeq .= " OR b.slug LIKE '%{$key}%')";
        $this->db->where($likeq);
        $this->db->limit($limit, $offset);
        $this->db->order_by('a.created_at', 'DESC');
        $this->db->order_by('a.updated_at', 'DESC');
        return $this->db->get()->result('RaoVat_Entity');
    }

    public function countSearchProduct($key){
        $result = null;
        $aWhere = array(
            'a.deleted_at' => null,
            'b.deleted_at' => null,
            'c.deleted_at' => null,
            'a.active_flg' => 1);
        $this->db->select('a.id');
        $this->db->from($this->table_product_name . ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_category_name . ' c', 'a.cate_id=c.id');
        $this->db->join($this->table_shop_name . ' d', 'a.shop_id=d.id');
        $this->db->where($aWhere);
        $likeq = "a.title LIKE '%{$key}%'";
        $likeq .= " OR a.slug LIKE '%{$key}%'";
        $likeq .= " OR b.code LIKE '%{$key}%'";
        $likeq .= " OR b.price_old LIKE '%{$key}%'";
        $likeq .= " OR b.price_new LIKE '%{$key}%'";
        $likeq .= " OR b.image LIKE '%{$key}%'";
        $likeq .= " OR b.description LIKE '%{$key}%'";
        $likeq .= " OR c.title LIKE '%{$key}%'";
        $likeq .= " OR c.slug LIKE '%{$key}%'";
        $likeq .= " OR d.title LIKE '%{$key}%'";
        $likeq .= " OR d.slug LIKE '%{$key}%'";
        $likeq = "(" . $likeq . ")";
        $this->db->where($likeq);
        return $this->db->count_all_results();
    }

    public function countSearchPost($key){
        $select = "a.id";
        $this->db->select($select);
        $this->db->from($this->table_post_name . ' AS a');
        $this->db->join($this->table_post_cate_name . ' b', 'b.id=a.post_cate_id');
        $this->db->where('a.publish_flg', ON);
        $this->db->where('a.deleted_at', NULL);
        $this->db->where('b.deleted_at', NULL);
        $likeq = "(a.title LIKE '%{$key}%'";
        $likeq .= " OR a.slug LIKE '%{$key}%'";
        $likeq .= " OR a.description LIKE '%{$key}%'";
        $likeq .= " OR a.content LIKE '%{$key}%'";
        $likeq .= " OR b.title LIKE '%{$key}%'";
        $likeq .= " OR b.slug LIKE '%{$key}%')";
        $this->db->where($likeq);
        return $this->db->count_all_results();
    }

    public function countSearchRaoVat($key){
        $select = "a.id";
        $this->db->select($select);
        $this->db->from($this->table_rao_vat_name . ' AS a');
        $this->db->join($this->table_category_name . ' b', 'b.id=a.cate_id');
        $this->db->join($this->table_rao_vat_place_name . ' c', 'c.id=a.place_id');
        $this->db->where('a.active_flg', ON);
        $this->db->where('a.deleted_at', NULL);
        $this->db->where('b.deleted_at', NULL);
        $likeq = "(a.title LIKE '%{$key}%'";
        $likeq .= " OR a.slug LIKE '%{$key}%'";
        $likeq .= " OR a.description LIKE '%{$key}%'";
        $likeq .= " OR a.detail LIKE '%{$key}%'";
        $likeq .= " OR c.name LIKE '%{$key}%'";
        $likeq .= " OR b.title LIKE '%{$key}%'";
        $likeq .= " OR b.slug LIKE '%{$key}%')";
        $this->db->where($likeq);
        return $this->db->count_all_results();
    }
}
?>